<?php

namespace App\Controller\Api\Wuliu;

use App\Base\BaseController4Restful;
use App\Entity\Wuliu\WuliuCars;
use App\Repository\Wuliu\WuliuCarsRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiWuliuCarsController extends BaseController4Restful
{
    /**
     * @Route("/api/wuliuCarss", name="apiGetWuliuCarss",methods={"GET","POST"})
     */
    public function apiGetWuliuCarss(WuliuCarsRepository $repository,Request $request)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $WuliuCars WuliuCars*/
                $WuliuCars = $this->createEntity($data,WuliuCars::class);
                $this->myEm()->persist($WuliuCars);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $WuliuCars);
            default:
                $data       = $this->getRequestData();
                $per_page   = $data["limit"]??10;
                $page       = $data["page"]??1;
                $where      = $data['_where']??[];
                $search     = $data['_search']??[];
                $order      = $data["_order"]??["sort"=>"ASC"];
//                dump($order);
                //列表筛选
                $qb = $repository->createQueryBuilder("A");
                createWhere($qb,$where);
                createSearch($qb,$search,[],"or");
                createOrderBy($qb,$order);
                $qb->setFirstResult($page*$per_page - $per_page)->setMaxResults($per_page);
                $list = $qb->getQuery()->execute();
                $list= $this->normalize($list);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/wuliuCarss/{id}", name="apiAddWuliuCarss",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateWuliuCarss(WuliuCars $WuliuCars,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($WuliuCars);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $WuliuCars WuliuCars*/
                $WuliuCars = $this->updateEntity($data,WuliuCars::class,$WuliuCars);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($WuliuCars);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $WuliuCars);
    }
}
